const app = getApp()
const plugin = requirePlugin("WechatSI")
const manager = plugin.getRecordRecognitionManager()
let backgroundAudioManager = wx.getBackgroundAudioManager()
import anaScore from '../../utils/anaScore.js'
import dscores from '../../utils/data.js'
Page({
    data: {
        type:"",
        isBegin: true,
        allDisabilityType:["儿麻后遗症", "骨关节疾患", "畸形", "脊柱脊髓伤病", "截/缺肢", "截瘫", 
      "精神", "脑瘫", "偏瘫", "视力", "听力", "言语", "肢体", "智力", "周围神经损伤"],
        gender: ["男","女"],
        allDisabilityCause:["未获得","产伤", "创伤或意外伤害", "发育畸形", "感染", "骨关节病", "疾病", "脊髓灰质炎",
     "脊髓损伤", "交通事故", "脑性瘫痪", "脑血管疾病", "其他外伤", "先天性损伤或发育障碍", "原因不明", 
     "早产", "窒息", "中毒"],
     allDisabilityLevel:["未定级","一级","二级","三级","四级"],
        answerList: ["能够独立完成","需要大部分帮助","需要小部分帮助","完全依赖帮助","请再说一次"],
        currentText:'',
        thisQuestion: '',
        thisQuestionFlag: 0,
        thisTitle: "",
        imgsrc:"",
        beginTag:0,
        endTag:12,
    },

    onLoad: function (options) {
        if (options.type == "运动功能"){
            this.setData({
                beginTag:0,
                endTag:4
            })
        }
        if (options.type == "生活自理"){
            this.setData({
                beginTag:5,
                endTag:8
            })
        }
        if (options.type == "社会适应"){
            this.setData({
                beginTag:9,
                endTag:12
            })
        }
        this.data.type = options.type
        this.setQuestion(this.data.beginTag)
        this.initRecord()
        backgroundAudioManager.onEnded(() =>{
            this.streamRecord()
        })
    },
    setQuestion(ques) {
        this.setData({
            thisQuestion: this.data.questionList[ques].question,
            thisQuestionFlag: ques,
            imgsrc:this.data.questionList[ques].img,
            thisTitle: this.data.questionList[ques].title,
        })
    },
    //选择答案
    finish(event) {
        dscores[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(event.target.dataset.score)
        // app.globalData.scores[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(event.target.dataset.score)
        if(this.data.thisQuestionFlag <this.data.endTag ){
            wx.showToast({
                title: this.data.answerList[3-event.target.dataset.score],
            })
            this.toNext()
        }else{
            wx.navigateBack({
                url: '../question/question',
            })
        }

    },
    //选择上一个问题或下一个问题
    toLast(){
        if(this.data.thisQuestionFlag > this.data.beginTag)
            this.setQuestion(this.data.thisQuestionFlag-1)
        else
            wx.showToast({
                title: '没有问题啦',
                icon: 'error',
            })
    },
    toNext(){
        if(this.data.thisQuestionFlag < this.data.endTag)
            this.setQuestion(this.data.thisQuestionFlag+1)
        else
            wx.showToast({
                title: '没有问题啦',
                icon: 'error'
            })
    },
    //语音输入模块
    //文字转语音
    playsound:async function(con) {
        plugin.textToSpeech({
            lang: "zh_CN",
            tts: true,
            content: con,
            success: function(res) {
                backgroundAudioManager.src = res.filename
                backgroundAudioManager.title = con
            },
            fail: function(res) {
                console.log("fail tts", res)
            }
        })
    },

    //录音功能
    streamRecord: function() {
        manager.start({
            lang: 'zh_CN',
        })
    },
    streamRecordEnd:async function() {
        manager.stop()
        console.log("end:"+this.data.currentText)
        console.log(this.data)
        let ans = await anaScore(this.data.currentText)
        if(ans!=-1){
            dscores[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(ans)
            // app.globalData.scores[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(ans)
            this.toNext()
        }
        wx.showToast({
            title: this.data.answerList[3-ans],
            icon: 'error'
        })
        console.log(ans)
    },
    //语音转文字
    initRecord: function() {
        //有新的识别内容返回，则会调用此事件
        manager.onRecognize = (res) => {
            let text = res.result
            this.setData({
                currentText: text
            })
        }
        manager.onStop = (res) => {
            let text = res.result
            this.setData({
                currentText: text
            })
        }
    },
})